package com.zik.rader_and_video.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.zik.rader_and_video.config.MyResponse;
import com.zik.rader_and_video.po.Errors;
import com.zik.rader_and_video.po.Room;
import com.zik.rader_and_video.service.IErrorsService;
import com.zik.rader_and_video.service.IRoomService;
import com.zik.rader_and_video.utils.HttpMqttUtils;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;

/**
 * @author zik
 * @description
 * @time 2023/6/14 15:40
 **/
@RestController
@RequestMapping("/room")
public class RoomController {

    @Resource(name = "RoomService")
    private IRoomService roomService;

    @Resource(name = "IErrorsService")
    private IErrorsService errorsService;

    @GetMapping("getRoom")
    public MyResponse<Room> getRoom() {
        Room rooms = HttpMqttUtils.getRooms();
        return MyResponse.success(rooms);
    }

    @GetMapping("getNoise")
    public MyResponse<Integer> getNoise() {
        Integer noise =  HttpMqttUtils.getNoise();
        return MyResponse.success(noise);
    }


    @GetMapping("save")
    public MyResponse<Room> save() {
        Room rooms = HttpMqttUtils.getRooms();
        if (rooms != null && roomService.save(rooms)) {
            return MyResponse.success(rooms);
        }
        return MyResponse.error("查询失败");
    }

    @GetMapping("pageRoom/{page}/{pageSize}")
    public MyResponse<Page<Room>> getPage(@PathVariable Integer page,
                                          @PathVariable Integer pageSize) {
        Room rooms = HttpMqttUtils.getRooms();

        if (rooms == null){
            return MyResponse.error("未检测到rooms信息");
        }
        isError(rooms);
        if(roomService.save(rooms)){
            LambdaQueryWrapper<Room> queryWrapper = new LambdaQueryWrapper<>();
            queryWrapper
                    .orderByDesc(Room::getUpdateTime);
            return MyResponse.success(roomService.page(new Page<>(page, pageSize), queryWrapper));
        }else {
            return MyResponse.error("保存信息时出错");
        }
    }

    public void isError(Room room) {

        if(room.getTemperature() > 39){
            Errors errors = new Errors();
            errors.setErrorTag("Temperature");
            errors.setErrorData("温度过高");
            errors.setErrorValue(room.getTemperature());
            errorsService.save(errors);
        } else if(room.getTemperature() <10) {
            Errors errors = new Errors();
            errors.setErrorTag("Temperature");
            errors.setErrorData("温度过低");
            errors.setErrorValue(room.getTemperature());

            errorsService.save(errors);
        }
        if(room.getHumidity() > 50){
            Errors errors = new Errors();
            errors.setErrorTag("Humidity");
            errors.setErrorData("湿度过高");
            errors.setErrorValue(room.getHumidity());

            errorsService.save(errors);

        }else if(room.getHumidity() < 30) {
            Errors errors = new Errors();
            errors.setErrorTag("Humidity");
            errors.setErrorData("湿度过高");
            errors.setErrorValue(room.getHumidity());

            errorsService.save(errors);

        }
        if(room.getCarbon() > 24){
            Errors errors = new Errors();
            errors.setErrorTag("Carbon");
            errors.setErrorData("一氧化碳浓度高");
            errors.setErrorValue(room.getCarbon());

            errorsService.save(errors);

        }
        if(room.getMethanal() > 8){
            Errors errors = new Errors();
            errors.setErrorTag("getMethanal");
            errors.setErrorData("甲醛浓度高");
            errors.setErrorValue(room.getMethanal());

            errorsService.save(errors);

        }
        if(room.getUltraviolet() > 10) {
            Errors errors = new Errors();
            errors.setErrorTag("Ultraviolet");
            errors.setErrorData("紫外线强度高");
            errors.setErrorValue(room.getUltraviolet());

            errorsService.save(errors);
        }
    }

}
